package com.yywq.service.impl;

import com.yywq.error.BusinessException;
import com.yywq.error.EnumBusinessError;
import com.yywq.mapper.GoodPictureMapper;
import com.yywq.mapper.GoodsMapper;
import com.yywq.pojo.GoodPicture;
import com.yywq.pojo.Goods;
import com.yywq.pojo.model.GoodsInfo;
import com.yywq.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 吴磊
 * @program: yywq
 * @create: 2021-03-20 22:25
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodPictureMapper goodPictureMapper;

    /**
     * 返回商品详情
     *
     * @param goodsId
     * @return
     * @throws BusinessException
     */
    public GoodsInfo getGoodsInfo(String goodsId) throws BusinessException {
        Goods goods = goodsMapper.getGoodsById(goodsId);
        if (goods == null) {
            throw new BusinessException(EnumBusinessError.GOODS_NOT_EXIST);
        }
        List<GoodPicture> picture = goodPictureMapper.getGoodsPictureByGoodsId(goodsId);
        GoodsInfo goodsInfo = new GoodsInfo();
        BeanUtils.copyProperties(goods, goodsInfo);
        List<String> ps = null;
        if (picture != null && picture.size() > 0) {
            ps = picture.stream().map(p -> p.getGoodPicture()).collect(Collectors.toList());
        }
        goodsInfo.setGoodPicture(ps);
        return goodsInfo;
    }
}
